﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;


namespace MulticastTV
{
    /// <summary>
    /// Record stream to file
    /// </summary>
    public class Rec
    {
        DumpToFile recraw = new DumpToFile();

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
        [FlagsAttribute]
        public enum EXECUTION_STATE : uint
        {
            ES_SYSTEM_REQUIRED = 0x00000001,
            ES_DISPLAY_REQUIRED = 0x00000002,
            ES_CONTINUOUS = 0x80000000,
            ES_AWAYMODE_REQUIRED = 0x00000040
        }

        string recfolder, pre, name, path, ip, filename;
        string[] args_rec= new string[6];
        VideoLan.VideoLanClient vlc_sched;
        VideoLan.VlcMediaPlayer player_sched;

        /// <summary>
        /// Start recording
        /// </summary>
        /// <param name="recfolder_">Recording folder</param>
        /// <param name="pre_">Recording name prefix</param>
        /// <param name="name_">Recording name</param>
        /// <param name="path_">Application startup path</param>
        /// <param name="ip_">Multicast ip</param>
        /// <param name="filename_">Recording name postfix without extension</param>
        public void start(string recfolder_, string pre_, string name_, string path_,  string ip_, string filename_) 
        {
            recfolder = recfolder_;
            pre = pre_;
            name = name_;
            path = path_;
            ip = ip_;
            filename = filename_;
            if (filename.Length > 0)
                filename = "-" + filename;

           filename = pre +name + filename;
           filename = Epg.validateFileName(filename);
            if (filename.Length > 100)
                filename=filename.Substring(0, 100);
            
            SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED);
            Globals.RecInstanceCount++;

            vlc_sched = new VideoLan.VideoLanClient(path);
            player_sched = vlc_sched.NewMediaPlayer(new IntPtr());
            //rec a file:
            VideoLan.VlcMedia media1 = vlc_sched.NewMedia(System.Text.Encoding.UTF8.GetBytes("udp://@" + ip));

            //TODO: Check in channel settings and record last selected audio and subtitles
            
            //remux (selected audio and subtitles)
            //media1.AddOption(System.Text.Encoding.UTF8.GetBytes(":sout=#std{access=file,mux=" + Properties.Settings.Default.RecordingFormat + ",dst=" + recfolder + @"\" + filename + "." + Properties.Settings.Default.RecordingFormat + ".mpg}"));

            //raw
            media1.AddOption(System.Text.Encoding.UTF8.GetBytes(":demuxdump-file=" + recfolder + @"\" + filename + ".raw.mpg"));
            media1.AddOption(System.Text.Encoding.UTF8.GetBytes(":demux=dump"));

            //media1.AddOption(System.Text.Encoding.UTF8.GetBytes(":snapshot-path=" + Properties.Settings.Default.RecFolder));
            //media1.AddOption(":plugin-path=" + path + @"\plugins\");

            player_sched.Load(media1);
            media1.Dispose();
            player_sched.Play();
        }

        public void stop() 
        {
            player_sched.Stop();
            //timer exception !
            player_sched.Dispose();
            vlc_sched.Dispose();
            GC.Collect();

            Globals.RecInstanceCount--;
            if (Globals.RecInstanceCount==0)
                SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
        }
    }
}
